﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using YuuzleComic.Config;
using YuuzleComic.DBUtility;

public partial class _404 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request["a"])
            {
                case "404":
                    this.Process404();
                    break;
                default:
                    this.ProcessError();
                    break;
            }
        }
    }

    private void ProcessError()
    {
        PanelError.Visible = true;
        Header.Title = Request["a"] + "系统出错了";
    }

    private void Process404()
    {
        Panel404.Visible = true;
        Header.Title = Request["a"] + "您访问的页面没有找到";
        Response.Status = "404 Not Found";
        Response.StatusCode = 404;

        Match m = Regex.Match(Request.RawUrl, @"comic/([0-9]+)/([0-9]+)\.html(\?p\=([0-9]+))?$");
        if (m.Success)
        {
            SqlParameter[] parameter = new SqlParameter[] { 
                new SqlParameter("@id",int.Parse(m.Groups[1].Value))
            };
            DataSet ds = DbHelperSQL.Query("select top(10) a.id,a.mhmc,b.id zjid,b.zjmc from comics a join zj b on a.id=b.cid where a.id=@id order by b.ordernum desc", parameter);

            if (ds.Tables[0].Rows.Count > 0)
            {
                comic.Visible = true;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    DataRow dr = ds.Tables[0].Rows[i];
                    if (i == 0)
                    {
                        comic.InnerHtml += "<strong><a href=\"/comic/" + dr["id"] + "/\">" + dr["mhmc"] + "漫画</a></strong>最新章节：<br />";
                    }
                    comic.InnerHtml += "<a href=\"/comic/" + dr["id"].ToString() + "/" + dr["zjid"] + ".html\" title=\"" + dr["zjmc"] + "\">" + dr["zjmc"] + "</a> ";
                }
            }
        }
    }
}